/*
 * @Author: admin@attacker.club
 * @Date: 2022-10-11 01:08:33
 * @LastEditTime: 2022-11-11 00:00:13
 * @Description:
 */
// 导入router的路由模式
import {
  createRouter,
  createWebHistory,
  // createWebHashHistory
} from "vue-router";

//导入进度条组件
import NProgress from "nprogress";
import "nprogress/nprogress.css";
// //引入整体布局Layout
import Layout from "@/layout";

/**
 * constantRoutes
 * 没有权限要求的基本页面
 * 所有角色都可以访问
 */
export const constantRoutes = [
{
    path: "/webssh",
    name: "WSTerminal",
    hidden: true,
    component: () => import("@/views/webterminal/WSTerminal"),
    meta: { title: "webssh" }, //meta原信息
  },
  {
    path: "/login", //url路径
    component: () => import("@/views/login/Login.vue"), //导入视图组件,
    meta: { title: "登录" }, //meta原信息
  },
  // dashboard
  {
    path: "/", // URL
    component: Layout, // 引用组件
    redirect: "/dashboard", // 访问首页跳转到仪表盘
    children: [
      {
        path: "/dashboard",
        component: () => import("@/views/dashboard"),
        name: "仪表盘",
        icon: "HomeFilled",
        meta: {
          title: "Dashboard",
        },
      },
    ],
  },
  // 个人中心页
  {
    path: "/profile",
    component: Layout,
    redirect: "/profile/index",
    children: [
      {
        path: "/profile",
        component: () => import("@/views/information"),
        name: "个人中心",
        icon: "Avatar",
        meta: {
          title: "个人中心",
        },
      },
    ],
  },

    // 资产管理
  {
    path: "/asset",
    component: Layout,
    name: "资产管理",
    icon:"ElementPlus",
    meta: {
      title: "资产管理",
      icon: "resources",
    },
    children: [
      {
        path: "/asset/servers",
        component: () => import("@/views/assets/servers"),
        name: "主机资源",
        meta: {
          title: "主机资源",
          //   icon: "server",
        //   noCache: true,
        },
      },
        {
        path: "/asset/credentials",
        component: () => import("@/views/assets/credentials"),
        name: "SSH凭据",
        meta: {
          title: "SSH凭据",
          noCache: true,
        },
      },
      
    ],
  },

   {
    // 平台管理
    path: "/platform",
    component: Layout,
    name: "平台管理",
    icon: "MostlyCloudy",
    children: [
      {
        path: "/platform/platforms",
        component: () => import("@/views/cloudPlatform/platform"),
        name: "平台",
        meta: {
          title: "平台",
        },
      },
      {
        path: "/platform/region",
        component: () => import("@/views/cloudPlatform/region"),
        name: "区域",
        meta: {
          title: "区域",
        },
      },
      {
        path: "/platform/account",
        component: () => import("@/views/cloudPlatform/account"),
        name: "云账号",
      
        meta: {
        //   permissions: ["admin", "platform-platformAccount"],
          title: "平台账号",
          noCache: true,
        },
      },
    ],
  },
  


];
/**
 * asyncRoutes
 * 需要根据用户权限判断动态加载的路由
 */
export const asyncRoutes = [

  {
    path: "/nav",
    name: "demo",
    icon: "CollectionTag",
    // 导入组件方式2：当路由被访问时才会加载组件（惰性）
    component: Layout,
    children: [
      {
        path: "/nav/a",
        name: "页面A",
        component: () => import("@/views/test/a"),
      },
      {
        path: "/nav/b",
        name: "页面B",
        component: () => import("@/views/test/b"),
      },
      {
        path: "/nav/c",
        name: "页面C",
        component: () => import("@/views/test/c"),
      },
      {
        path: "/nav/z",
        name: "页面z",
        component: () => import("@/views/test/z"),
      },
    ],
  },
];
//createRouter 创建路由实例
const router = createRouter({
  // hash模式：createWebHashHistory, // http://localhost:8080/#/workload/pod
  // history模式：createWebHisotry   // http://localhost:8080/workload/pod
  scrollBehavior: () => ({ y: 0 }), // 路由跳转后页面回到顶部
  history: createWebHistory(),
  routes: constantRoutes,
});
//进度条初始化
//递增进度条，这将获取当前状态值并添加0.2到状态0.994
NProgress.inc(100);
//easing 动画字符串
//speed 动画速度
//showSpinner 进度环显示隐藏
NProgress.configure({ easing: "ease", speed: 600, showSpinner: false });

router.beforeEach((to, from, next) => {
  //启动进度条
  NProgress.start();
  //设置头部
  if (to.meta.title) {
    document.title = to.meta.title;
  } else {
    document.title = "运维管理平台";
  }
  // 放行规则 ； 默认放行 next()
  if (from.path != "/") {
    // 打印跳转信息
    console.log("to:", to.path, "from:", from.path);
  }
  // 如果用户访问登录页，直接放行
  if (to.path == "/login") {
    return next();
  }
  // 获取是否存在token
  const token = window.sessionStorage.getItem("token");
  if (token) {
    next(); // 判断正常跳转
  } else {
    return next("/login"); // 没有token跳转到登录页
  }
});
router.afterEach(() => {
  //关闭进度条
  NProgress.done();
});
export function resetRouter() {
  const newRouter = createRouter(); // 新路由实例matcer，赋值给旧路由实例的matcher，（相当于replaceRouter）
  router.matcher = newRouter.matcher; // reset router
}
//抛出路由示例，在main.js中引用
export default router;
